package com.starter.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.starter.home.domain.entity.Friends;
import com.starter.home.domain.entity.LoginLog;
import com.starter.home.domain.entity.User;
import com.starter.home.service.IFriendsService;
import com.starter.home.service.IIndexService;
import com.starter.home.service.ILoginLogService;
import com.starter.home.service.IUserService;
import com.starter.home.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 */
@Service
public class IndexServiceImpl implements IIndexService {
    @Autowired
    private IUserService userService;

    @Autowired
    private IFriendsService friendsService;

    @Autowired
    private ILoginLogService loginLogService;

    @Override
    public Map<String, Object> getIndexForAdminData(Integer userId) {
        ConcurrentHashMap<String,Object> result = new ConcurrentHashMap<>();
        // 获取本系统全部用户
        Integer integer = userService.getBaseMapper().selectCount(null);
        result.put("totalUserCount",integer);

        String weekStart = DateUtil.getStartOrEndDayOfWeek(LocalDate.now(),true);
        String weekEnd = DateUtil.getStartOrEndDayOfWeek(LocalDate.now(),false);
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.ge("create_time",weekStart);
        queryWrapper.lt("create_time",weekEnd);
        Integer integer1 = userService.getBaseMapper().selectCount(queryWrapper);
        result.put("weekCount",integer1);

        //一周新增
        QueryWrapper<Friends> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("from_id",userId);
        result.put("myFriendCount",friendsService.getBaseMapper().selectCount(queryWrapper1));

        QueryWrapper<LoginLog> queryWrapper2 = new QueryWrapper<>();
        result.put("allCount",loginLogService.getBaseMapper().selectCount(queryWrapper2));

        result.put("weekUserCountList", getUserCountList());


        result.put("visitCount", getVisitCount(null));
        return result;
    }

    @Override
    public Map<String, Object> getUserIndex(Integer userId) {
        ConcurrentHashMap<String,Object> result = new ConcurrentHashMap<>();

        QueryWrapper<Friends> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("from_id",userId);
        result.put("myFriendCount",friendsService.getBaseMapper().selectCount(queryWrapper));

        QueryWrapper<Friends> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("from_id",userId);
        queryWrapper1.eq("status",1);
        result.put("myFriendApply",friendsService.getBaseMapper().selectCount(queryWrapper1));

        QueryWrapper<Friends> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("to_id",userId);
        queryWrapper3.eq("status",1);
        result.put("myFriendApply",friendsService.getBaseMapper().selectCount(queryWrapper3));

        QueryWrapper<LoginLog> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_id",userId);
        result.put("myVisitCount",loginLogService.getBaseMapper().selectCount(queryWrapper2));

        result.put("visitCount", getVisitCount(userId));
        return result;
    }

    private int[] getVisitCount(Integer userId) {
        LocalDate today = LocalDate.now();
        DayOfWeek week = today.getDayOfWeek();
        int value = week.getValue();
        LocalDate localDate = today.minusDays(value - 1);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            list.add(localDate.plusDays(i).toString());
        }
        int[] result = new int[7];
        for (int i = 0; i < list.size(); i++) {
            int count = loginLogService.getVisitCount(list.get(i),userId);
            result[i] = count;
        }
        return result;
    }

    private int[] getUserCountList() {
        LocalDate today = LocalDate.now();
        DayOfWeek week = today.getDayOfWeek();
        int value = week.getValue();
        LocalDate localDate = today.minusDays(value - 1);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            list.add(localDate.plusDays(i).toString());
        }
        int[] result = new int[7];
        for (int i = 0; i < list.size(); i++) {
            int count = userService.getAddUserCount(list.get(i));
            result[i] = count;
        }
        return result;
    }
}
